package com.chenexample.myshop16173.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.chenexample.myshop16173.R;
import com.chenexample.myshop16173.shuju;
import com.chenexample.myshop16173.util.SPUtils;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MyRVAdapter2 extends RecyclerView.Adapter<MyRVAdapter2.MyTVHolder> {

    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    List<shuju> mData;

    public MyRVAdapter2(Context context, List<shuju> data) {
        mLayoutInflater = LayoutInflater.from(context);
        mContext = context;
        mData = data;

    }

    @Override
    public MyRVAdapter2.MyTVHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyRVAdapter2.MyTVHolder(mLayoutInflater.inflate(R.layout.rv_txt_item, parent, false));
    }


    @Override
    public void onBindViewHolder(final MyRVAdapter2.MyTVHolder holder, final int pos) {
        holder.tv1.setText(mData.get(pos).getCanyurenshu() + "/" + mData.get(pos).getCyrenshu());
        holder.tv2.setText(mData.get(pos).getZuji());
        holder.tv3.setText(mData.get(pos).getHdmz());
        holder.bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    new QMUIDialog.MessageDialogBuilder(mContext)
                            .setTitle("报名")
                            .setMessage("确定报名" + mData.get(pos).getHdmz())
                            .addAction("取消", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    dialog.dismiss();

                                }
                            })
                            .addAction("确定", new QMUIDialogAction.ActionListener() {
                                @Override
                                public void onClick(QMUIDialog dialog, int index) {
                                    Toast.makeText(mContext, "报名成功，请等待审核", Toast.LENGTH_LONG).show();
                                    int a = (int) SPUtils.get(mContext, "jifen", 0);
                                    SPUtils.put(mContext, "jifen", a + 10);
                                    dialog.dismiss();
                                }
                            })
                            .show();

            }
        });

        if (pos == 0) {
            Glide.with(mContext)
                    .load(R.drawable.qwe)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))//圆角半径
                    .into(holder.imageView);
        } else if (pos == 1) {
            Glide.with(mContext)
                    .load(R.drawable.qwe1)
                    .apply(RequestOptions.bitmapTransform(new RoundedCorners(60)))//圆角半径
                    .into(holder.imageView);
        }
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    class MyTVHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView tv1, tv2, tv3;
        Button bt1;

        MyTVHolder(View itemView) {
            super(itemView);
            tv1 = itemView.findViewById(R.id.tv1);
            tv2 = itemView.findViewById(R.id.tv2);
            tv3 = itemView.findViewById(R.id.tv3);
            bt1 = itemView.findViewById(R.id.bt1);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}